//地图加载类
var sw_servicetype = {
    sw_tile: "tile",
    sw_vector: "vector",
    sw_terrain: "terrain",
    sw_oblique: "oblique",
    sw_gltf: "gltf",
    sw_poi: "poi"
};
var sw_servertype = {
    sw_geoserver: "geoserver",
    sw_mongodb: "mongodb",
    sw_iis: "iis",
    sw_arcgis: "arcgis"
};
var sw_source = {
    sw_xyz: "xyz",
    sw_arcgistile: "tilearcgisrest",
    sw_arcgis: "arcgis",
    sw_osm: "osm",
    sw_wms: "tilewms",
    sw_wmts: "wmts",
    sw_pbf: "pbftile",
    sw_mongodb: "mongodb",
    sw_baidu: "baidu",
    sw_gaode: "gaode",
    sw_google: "google",
    sw_tianditu: "tianditu",
    sw_template: "template"
};

function LayerHelper(layerInfo) {
    var LayerHelper = Object;
    LayerHelper.layerInfo = layerInfo;
    LayerHelper.isLoad = false;
    LayerHelper.GetLayer = function() {
        var thisLayerInfo = this.layerInfo;
        var isLoad = this.isLoad;
        var id = thisLayerInfo.hasOwnProperty("GUID") ? thisLayerInfo.GUID : "";
        return cesium.layer3DList[id];
    }
    LayerHelper.moveToUp = function() {
        viewer.imageryLayers.raise(cesium.layer3DList[this.layerInfo.GUID]);
        //viewer.imageryLayers.raiseToTop(cesium.layer3DList[this.layerInfo.GUID]);
    }
    LayerHelper.moveToDown = function() {
        viewer.imageryLayers.lower(cesium.layer3DList[this.layerInfo.GUID]);
        //viewer.imageryLayers.lowerToBottom(cesium.layer3DList[this.layerInfo.GUID]);
    }
    LayerHelper.addLayer = function(visible) {
        //#region 获取图层信息
        var thisLayerInfo = this.layerInfo;
        var isLoad = this.isLoad;
        var myLayer = null;
        var layerCount = 0;

        var id = thisLayerInfo.hasOwnProperty("GUID") ? thisLayerInfo.GUID : "";
        var layers = viewer.scene.imageryLayers;
        var primitives = viewer.scene.primitives;
        if (!cesium.layer3DList[id] && (typeof(visible) == "undefined" || visible)) {
            myLayer = createLayer(thisLayerInfo, layerCount);
            if (myLayer) {
                myLayer.NAME = thisLayerInfo.NAME;
                cesium.layer3DList[id] = myLayer;
                isLoad = true;
            }
            change3DLayerTransparent(id, m_LoadLayerInfo);
        } else {
            if (typeof(visible) != "undefined") {
                if (!visible) {
                    deleteServerTypeMap(id, thisLayerInfo);
                } else {
                    if (thisLayerInfo.SERVICETYPE == sw_servicetype.sw_poi) {
                        if (cesium.layer3DList[id]) {
                            var entities = cesium.layer3DList[id].entities.values;
                            for (var i = 0; i < entities.length; i++) {
                                entities[i].show = true;
                            }
                        }
                    } else {
                        cesium.layer3DList[id].show = true;
                    }
                }
            } else {
                deleteServerTypeMap(id, thisLayerInfo);
            }
        }
        this.isload = isLoad;
        return myLayer;
    }

    LayerHelper.changeLayerTransparent = function(id, thisLayerInfo, opacity) {
        layerTransparent[id] = opacity;
        var thisLayer = cesium.layer3DList[id];
        var servicetype = thisLayerInfo.hasOwnProperty("SERVICETYPE") ? thisLayerInfo.SERVICETYPE : "tile"; //图层类型
        var source = thisLayerInfo.hasOwnProperty("SOURCE") ? thisLayerInfo.SOURCE : "";
        switch (servicetype) {
            case sw_servicetype.sw_tile: //切片
                {
                    switch (source) {
                        case sw_source.sw_xyz:
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_arcgis:
                        case sw_source.sw_arcgistile:
                            //arcgisserver-Arcgis切片服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_osm:
                            // osm开源世界地图（OpenStreetMap）服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_wms:
                            //wms网络地图服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_wmts:
                            //wmts-web地图瓦片服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_pbf:
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_mongodb:
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_baidu:
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_gaode:
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_google:
                            //mongodb自定义服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_tianditu:
                            //mongodb自定义服务处理
                            thisLayer.alpha = opacity;
                            break;
                        case sw_source.sw_template:
                            thisLayer.alpha = opacity;
                            break;
                        default:
                            break;
                    }
                    break;
                }
            case sw_servicetype.sw_terrain: //地形
                {
                    //viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});
                    console.log("地形不支持透明度调整！");
                    break;
                }
            case sw_servicetype.sw_oblique: //倾斜模型
                {
                    thisLayer.style = new Cesium.Cesium3DTileStyle({
                        color: "color('rgba(255,255,255," + opacity + ")')",
                    });
                    break;
                }
            case sw_servicetype.sw_gltf: //倾斜模型
                {
                    thisLayer.style = new Cesium.Cesium3DTileStyle({
                        color: "color('rgba(255,255,255," + opacity + ")')",
                    });
                    break;
                }
            case "undefined":
                break;
            default:
                break;
        }
    }

    function createLayer(myLayerInfo, layerIndex) {
        var layerType = 3;
        var thisLayer = null;
        var thisService = null;
        //属性
        var servicetype = myLayerInfo.hasOwnProperty("SERVICETYPE") ? myLayerInfo.SERVICETYPE : "tile"; //图层类型
        var source = myLayerInfo.hasOwnProperty("SOURCE") ? myLayerInfo.SOURCE : "";
        var status = myLayerInfo.hasOwnProperty("STATUS") ? myLayerInfo.STATUS : "";;
        var isOrder = true;

        var sw_status = {
            sw_online: "online",
            sw_offline: "offline"
        };
        switch (servicetype) {
            case sw_servicetype.sw_tile: //切片
                //#region 根据不同数据源进行加载设置
                switch (source) {
                    case sw_source.sw_xyz:
                        //xyz-谷歌/高德服务处理
                        if (status == sw_status.sw_online) {
                            thisService = new TileXYZService(myLayerInfo);
                        } else if (status == sw_status.sw_offline) {

                        }
                        break;
                    case sw_source.sw_arcgis:
                        //arcgisserver-Arcgis切片服务处理
                        thisService = new ArcgisService(myLayerInfo);
                        break;
                    case sw_source.sw_arcgistile:
                        //arcgisserver-Arcgis切片服务处理
                        thisService = new TileArcgisService(myLayerInfo);
                        break;
                    case sw_source.sw_osm:
                        // osm开源世界地图（OpenStreetMap）服务处理
                        thisService = new TileOSMService(myLayerInfo);
                        break;
                    case sw_source.sw_wms:
                        //wms网络地图服务处理
                        thisService = new TileWMSService(myLayerInfo);
                        break;
                    case sw_source.sw_wmts:
                        //wmts-web地图瓦片服务处理
                        thisService = new TileWMTSService(myLayerInfo);
                        break;
                    case sw_source.sw_pbf:
                        //pbf矢量切片服务处理
                        thisService = new TilePbfService(myLayerInfo);
                        break;
                    case sw_source.sw_mongodb:
                        //mongodb自定义服务处理
                        thisService = new TileMongodbService(myLayerInfo);
                        break;
                    case sw_source.sw_baidu:
                        //百度服务处理
                        thisService = new BaiduMapService(myLayerInfo);
                        break;
                    case sw_source.sw_gaode:
                        //高德服务处理
                        thisService = new GaodeMapService(myLayerInfo);
                        break;
                    case sw_source.sw_google:
                        //谷歌服务处理
                        thisService = new GoogleMapService(myLayerInfo);
                        break;
                    case sw_source.sw_tianditu:
                        //天地图服务处理
                        thisService = new TiandituMapService(myLayerInfo);
                        break;
                    case sw_source.sw_template:
                        //模板服务处理
                        thisService = new TemplateMapService(myLayerInfo);
                        break;
                    default:
                        break;
                }
                break;
            case sw_servicetype.sw_vector: //矢量
                thisService = new VectorService(myLayerInfo);
                break;
            case sw_servicetype.sw_terrain: //地形
                thisService = new TerrainService(myLayerInfo);
                isOrder = false;
                break;
            case sw_servicetype.sw_oblique: //倾斜
                thisService = new ObliqueService(myLayerInfo);
                isOrder = false;
                break;
            case sw_servicetype.sw_gltf: //gltf
                thisService = new GltfService(myLayerInfo);
                isOrder = false;
                break;
            case sw_servicetype.sw_poi: //poi
                thisService = new PoiService(myLayerInfo);
                isOrder = false;
                break;
        }
        if (thisService != null) {
            thisLayer = thisService.CreateLayer();
        }

        //对图层类型进行绑定
        thisLayer["NAME"] = myLayerInfo["ALIAS"];
        thisLayer["URL"] = myLayerInfo["URL"];
        thisLayer["SERVICETYPE"] = servicetype;
        thisLayer["SOURCE"] = source;
        return thisLayer;
    }
    return LayerHelper;
}

/**
 * 删除指定ID的图层
 */
function deleteServerTypeMap(id, layerInfo) {
    var servicetype = layerInfo.hasOwnProperty("SERVICETYPE") ? layerInfo.SERVICETYPE : "tile"; //图层类型
    var source = layerInfo.hasOwnProperty("SOURCE") ? layerInfo.SOURCE : "";
    switch (servicetype) {
        case sw_servicetype.sw_tile: //切片
            {
                switch (source) {
                    case sw_source.sw_xyz:
                        //xyz-谷歌/高德服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_arcgis:
                    case sw_source.sw_arcgistile:
                        //arcgisserver-Arcgis切片服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_osm:
                        // osm开源世界地图（OpenStreetMap）服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_wms:
                        //wms网络地图服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_wmts:
                        //wmts-web地图瓦片服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_pbf:
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        //pbf矢量切片服务处理
                        break;
                    case sw_source.sw_mongodb:
                        //mongodb自定义服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_baidu:
                        //mongodb自定义服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_gaode:
                        //mongodb自定义服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_google:
                        //mongodb自定义服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_tianditu:
                        //mongodb自定义服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                    case sw_source.sw_template:
                        //模板服务处理
                        viewer.scene.imageryLayers.remove(cesium.layer3DList[id]);
                        delete cesium.layer3DList[id];
                        break;
                }
                break;
            }
        case sw_servicetype.sw_terrain: //地形
            {
                viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});
                delete cesium.layer3DList[id];
                break;
            }
        case sw_servicetype.sw_oblique: //倾斜模型
            {
                cesium.layer3DList[id].show = false;
                break;
            }
        case sw_servicetype.sw_gltf: //gltf
            cesium.layer3DList[id].show = false;
            break;
        case sw_servicetype.sw_poi: //gltf
            if (cesium.layer3DList[id]) {
                var entities = cesium.layer3DList[id].entities.values;
                for (var i = 0; i < entities.length; i++) {
                    entities[i].show = false;
                }
            }
            break;
        case "undefined":
            break;
        default:
            break;
    }
}

/**
 * 指定ID的图层透明度设置
 */
function change3DLayerTransparent(id, layerInfo) {
    var thisLayerInfo = layerInfo[id];
    if(!thisLayerInfo) {return;}
    var opacity = layerTransparent[id] ? layerTransparent[id] : 1;
    var thisLayer = cesium.layer3DList[id];
    var servicetype = thisLayerInfo.hasOwnProperty("SERVICETYPE") ? thisLayerInfo.SERVICETYPE : "tile"; //图层类型
    var source = thisLayerInfo.hasOwnProperty("SOURCE") ? thisLayerInfo.SOURCE : "";
    switch (servicetype) {
        case sw_servicetype.sw_tile: //切片
            {
                switch (source) {
                    case sw_source.sw_xyz:
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_arcgis:
                    case sw_source.sw_arcgistile:
                        //arcgisserver-Arcgis切片服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_osm:
                        // osm开源世界地图（OpenStreetMap）服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_wms:
                        //wms网络地图服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_wmts:
                        //wmts-web地图瓦片服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_pbf:
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_mongodb:
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_baidu:
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_gaode:
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_google:
                        //mongodb自定义服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_tianditu:
                        //mongodb自定义服务处理
                        thisLayer.alpha = opacity;
                        break;
                    case sw_source.sw_template:
                        thisLayer.alpha = opacity;
                        break;
                    default:
                        break;
                }
                break;
            }
        case sw_servicetype.sw_terrain: //地形
            {
                //viewer.scene.terrainProvider = new Cesium.EllipsoidTerrainProvider({});
                console.log("地形不支持透明度调整！");
                break;
            }
        case sw_servicetype.sw_oblique: //倾斜模型
            {
                thisLayer.style = new Cesium.Cesium3DTileStyle({
                    color: "color('rgba(255,255,255," + opacity + ")')",
                });
                break;
            }
        case sw_servicetype.sw_gltf: //gltf
            {
                thisLayer.style = new Cesium.Cesium3DTileStyle({
                    color: "color('rgba(255,255,255," + opacity + ")')",
                });
                break;
            }
        case "undefined":
            break;
        default:
            break;
    }
}
var layerTransparent = {};

function checkLayerTransparent(layer, id) {
    if (layerTransparent[id]) layer.alpha = layerTransparent[id];
}

//定位
function flyToLocation(lon, lat, zoom, rotate) {
    var places = ol.proj.fromLonLat([parseFloat(lon), parseFloat(lat)]);
    if (rotate != undefined) {
        view.animate({
            center: places,
            zoom: zoom - 5 > 0 ? zoom - 5 : 1,
            duration: 2000,
            rotation: parseFloat(rotate)
        });
        view.animate({
            center: places,
            zoom: zoom,
            duration: 2000,
            rotation: parseFloat(rotate)
        });
    } else {
        zoom = zoom ? zoom : parseInt(view.getZoom()); //缩放级别为空时，只平移不缩放
        setTimeout(function() {
            view.animate({
                center: places,
                zoom: parseInt(zoom),
                duration: 1200
            });
        }, 801);
    }
}